/*
 * <p>Title:       箄1�7单的标题</p>
 * <p>Description: 稍详细的说明</p>
 * <p>Copyright:   Copyright (c) 2004</p>
 * <p>Company:     东软集团股份有限公司</p>
 */
package com.ll.core.httpbase;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.Map;

import com.ll.core.model.RequestEnvelope;
import com.ll.core.util.JSONTool;

public class DefaultHttpHandlerImpl extends HttpHandler {
	private static class Cookie {
		private static String cookieValue = null;
	};

	public DefaultHttpHandlerImpl(HttpUtil hu) {
		super(hu);
	}

	public void dealCookie(HttpURLConnection httpConnection) {

		String cookieVal = null;
		String sessionId = null;
		String key = null;
		for (int i = 1; (key = httpConnection.getHeaderFieldKey(i)) != null; i++) {
			if (key.equalsIgnoreCase("set-cookie")) {
				cookieVal = httpConnection.getHeaderField(i);
				cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
				sessionId = sessionId + cookieVal + ";";
			}
		}
		Cookie.cookieValue = sessionId;
	
	}

	@Override
	public HttpResponse getResponsehandler(HttpURLConnection httpConnection)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				httpConnection.getInputStream(),"UTF-8"));
		this.dealCookie(httpConnection);
		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		br.close();
		HttpResponse httpResponse = new HttpResponse();
		httpResponse.setContext(sb.toString());
		httpResponse.setCode(0);
		return httpResponse;
	}

	@Override
	public void sendRequesthandler(HttpURLConnection httpConnection,
			HttpRequest httpRequest) throws Exception {
		RequestEnvelope<String,String> hrq = new RequestEnvelope<String,String>();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				httpConnection.getOutputStream()));
		StringBuffer sb = new StringBuffer();
		for (Object object : httpRequest.getParameters().entrySet()) {
			Map.Entry entry = (Map.Entry) object;
//			if(entry.getKey() == "NULL"){
//				sb.append(entry.getValue().toString());
//			}
			
			hrq.addParameter(entry.getKey().toString(), entry.getValue().toString());	
		}
		sb.append(JSONTool.ObjectToJSON(hrq));
		
		bw.write(sb.toString());
		bw.flush();
		bw.close();
	}

	@Override
	public void beforerequest(HttpURLConnection httpconnection)
			throws Exception {
		httpconnection.setDoOutput(true);
		// 设置是否从httpUrlConnection读入，默认情况下是true;
		httpconnection.setDoInput(true);
		// Post 请求不能使用缓存
		httpconnection.setUseCaches(false);
		httpconnection.setRequestProperty("user-agent","mozilla/4.0 (compatible; msie 6.0; windows 2000)");
		httpconnection.setRequestProperty("Content-type",
				"application/x-java-serialized-object");
		if (Cookie.cookieValue != null) {
			httpconnection.setRequestProperty("Cookie", Cookie.cookieValue);
			System.out.println("cookieValue:"+Cookie.cookieValue);
		}
		httpconnection.setRequestMethod("POST");
	}

}
